//
//  UserInfoViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/10/27.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "UserInfoViewController.h"
#import "AuthViewController.h"
#import "UserDefaultUtil.h"
#import "MBProgressHUD+MJ.h"
#import "UserDefaultUtil.h"
#import "HttpReauestParam.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIColor+Hex.h"
#import "LoginViewController.h"
@interface UserInfoViewController ()

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.contentView.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
    
    self.user_photo.layer.masksToBounds = YES;
    self.user_photo.layer.cornerRadius = 20;
    [self.user_photo.layer setBorderWidth:2];
    [self.user_photo.layer setBorderColor:[UIColor whiteColor].CGColor];
    
    NSString *userImage = [UserDefaultUtil getData:USER_PHOTO];
    NSString *userName = [UserDefaultUtil getData:USER_NAME];
    NSString *userphone = [UserDefaultUtil getData:USER_PHONE];
    _user_name.text = userName;
    [_user_photo loadImage:[NSURL URLWithString:userImage]];
    _user_phone.text = userphone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [self getData];
    [super viewDidAppear:animated];
}
-(void)getData{
    NSString *userId = [UserDefaultUtil getUserId];
    NSDictionary *dic =@{@"identifier":userId};
    
    HttpReauestParam *param = [[HttpReauestParam alloc]initWithData:@"app_Authentication_information_get" content:[self dictionaryToJson:dic] urlType:0];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
        
        if ([data[@"success"] boolValue]) {
            
            data = data[@"result"];
            
            _user_service.text = data[@"serviceType"];
            _user_are.text = data[@"serviceRegion"];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)out_click:(id)sender {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注销" message:@"注销后将会删除本地记录，并且不会再接收推送消息" preferredStyle:UIAlertControllerStyleAlert];
    alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    // 添加按钮
   // __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        UIStoryboard *sb= [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
        LoginViewController *loginVc = [sb instantiateViewControllerWithIdentifier:@"login_view"];
        [self presentViewController:loginVc animated:YES completion:nil];
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

- (IBAction)auth_click:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"重新认证" message:@"重新认证后，您的信息将会重新审核" preferredStyle:UIAlertControllerStyleAlert];
    alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    // 添加按钮
   // __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"重新认证" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        UIStoryboard *sb= [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
        AuthViewController *authViewController = [sb instantiateViewControllerWithIdentifier:@"auth_view"];
        authViewController.type = @"2";
        [self.navigationController pushViewController:authViewController animated:YES];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

-(NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}
- (IBAction)back_click:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
